<?php
declare (strict_types=1);

namespace app\common\model;

class HistorySearch extends Base
{
    protected $name = 'history_search';
    //存搜素记录
    public static function setHistory($paramkey,$user){
        parent::startTrans();
        try {
            $find=parent::where(['is_del'=>0,'uid'=>$user->id])->find();
            $title[]=$paramkey;
            if(!empty($find)){
                $keyword=json_decode($find['title'],true);
               // print_r($keyword);die;
                if(!empty($keyword)){
                    if(!in_array($paramkey,$keyword)){
                        $keywordtitle= array_merge_recursive($keyword,$title);
                    }
                }else{
                    $keywordtitle[]=$paramkey;
                }
                $find->title=json_encode($keywordtitle);
                $find->save();
            } else {
                $data['create_time']=time();
                $data['uid']=$user->id;
                $data['title']=json_encode($title);
                parent::create($data);
            }
            parent::commit();
            return ['code' => 1];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
}
